package com.xiaoniu.mc.client.enums;

import com.xiaoniu.mc.client.exception.ProducerException;
import com.xiaoniu.mc.client.message.SendMessageWrapper;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @类描述：
 * @创建人：林继丰
 * @创建时间：2017/8/25 17:00
 * @版权：Copyright (c) 深圳市牛鼎丰科技有限公司-版权所有.
 */
public enum ProducerStrategy {
    SYNC {
        @Override
        public void sendMsg(SendMessageWrapper messageWrapper, DefaultMQProducer producer) throws UnsupportedEncodingException {
            Message message = getMessage(messageWrapper);
            try {
                SendResult result = producer.send(message);
                if (!result.getSendStatus().equals(SendStatus.SEND_OK)) {
                    throw new ProducerException("发送失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void sendMsgOrderly(SendMessageWrapper messageWrapper, DefaultMQProducer producer) throws UnsupportedEncodingException {
            Message message = getMessage(messageWrapper);
            try {
                SendResult result = producer.send(message, new MessageQueueSelector() {
                    @Override
                    public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                        return null;
                    }
                }, messageWrapper.getOrderId());
                if (!result.getSendStatus().equals(SendStatus.SEND_OK)) {
                    throw new ProducerException("发送失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    },

    ASYNC {
        @Override
        public void sendMsg(SendMessageWrapper messageWrapper, DefaultMQProducer producer) throws UnsupportedEncodingException {
            Message message = getMessage(messageWrapper);
            try {
                producer.send(message, messageWrapper.getSendCallback());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void sendMsgOrderly(SendMessageWrapper messageWrapper, DefaultMQProducer producer) throws UnsupportedEncodingException {
            Message message = getMessage(messageWrapper);
            try {
                producer.send(message, new MessageQueueSelector() {
                    @Override
                    public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                        return null;
                    }
                }, messageWrapper.getOrderId(), messageWrapper.getSendCallback());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    },
    ONEWAY {
        @Override
        public void sendMsg(SendMessageWrapper messageWrapper, DefaultMQProducer producer) throws UnsupportedEncodingException {
            Message message = getMessage(messageWrapper);
            try {
                producer.sendOneway(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void sendMsgOrderly(SendMessageWrapper messageWrapper, DefaultMQProducer producer) throws UnsupportedEncodingException {
            Message message = getMessage(messageWrapper);
            try {
                producer.sendOneway(message, new MessageQueueSelector() {
                    @Override
                    public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                        return null;
                    }
                }, messageWrapper.getOrderId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private static Message getMessage(SendMessageWrapper messageWrapper) throws UnsupportedEncodingException {
        Message message = new Message(messageWrapper.getTopic(), messageWrapper.getTags(), messageWrapper.getKeys(),
                messageWrapper.getMessage().getBytes(messageWrapper.getChartset()));
        if (messageWrapper.getLevel() != null) {
            message.setDelayTimeLevel(messageWrapper.getLevel().getDelayLevel());
        }
        return message;
    }

    ProducerStrategy() {
    }

    public void sendMsg(SendMessageWrapper messageWrapper, DefaultMQProducer producer) throws UnsupportedEncodingException {

    }

    public void sendMsgOrderly(SendMessageWrapper messageWrapper, DefaultMQProducer producer) throws UnsupportedEncodingException {

    }
}
